﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UnitMeshPointComponent : MonoBehaviour {

    private bool isShow = false;
    /// <summary>
    /// 当前选择的 骨骼目标 索引
    /// </summary>
    public int m_selectTransform = -1;

    public string m_TransformFindName;

    /// <summary>
    /// 骨骼目标
    /// </summary>
    public Transform m_boneTarget;
    public Vector3 mPosition;

    public bool IsShow
    {
        get
        {
            return isShow;
        }

        set
        {
            isShow = value;
            if (isShow && m_boneTarget)
            {
                mPosition = this.transform.position - m_boneTarget.position;
            }
        }
    }

    public void SetTransform(Transform boneTarget)
    {
        if (boneTarget)
        {
            mPosition = this.transform.position - boneTarget.position;
            m_boneTarget = boneTarget;
        }
    }
    public void ClearTransform()
    {
        if (m_boneTarget)
        {
            m_boneTarget = null;
            m_selectTransform = -1;
            mPosition = Vector3.zero;
        }
    }
    void Start () {
        if (m_boneTarget)
        {
            mPosition = this.transform.position - m_boneTarget.position;
        }
    }
	
	void Update () {
        if (m_boneTarget && IsShow)
        {
             // this.transform.localPosition = this.transform.TransformPoint(m_boneTarget.position + mPosition);
              //this.transform.localPosition = m_boneTarget.TransformDirection(mPosition);
              this.transform.position = m_boneTarget.position + m_boneTarget.rotation * (mPosition);
              this.transform.rotation = m_boneTarget.rotation;
        }
    }
}
